/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package service;

import java.util.Collection;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.persistence.EntityManager;
import jpa.entities.ItemPedido;
import jpa.entities.Pedido;
import jpa.entities.Usuario;
import converter.PedidoesConverter;
import converter.PedidoConverter;
import javax.persistence.PersistenceContext;
import javax.ejb.Stateless;

/**
 *
 * @author carlos
 */

@Path("/pedidoes/")
@Stateless
public class PedidoesResource {
    @javax.ejb.EJB
    private PedidoResource pedidoResource;
    @Context
    protected UriInfo uriInfo;
    @PersistenceContext(unitName = "LaCruz_v2PU")
    protected EntityManager em;
  
    /** Creates a new instance of PedidoesResource */
    public PedidoesResource() {
    }

    /**
     * Get method for retrieving a collection of Pedido instance in XML format.
     *
     * @return an instance of PedidoesConverter
     */
    @GET
    @Produces({"application/xml", "application/json"})
    public PedidoesConverter get(@QueryParam("start")
                                 @DefaultValue("0")
    int start, @QueryParam("max")
               @DefaultValue("10")
    int max, @QueryParam("expandLevel")
             @DefaultValue("1")
    int expandLevel, @QueryParam("query")
                     @DefaultValue("SELECT e FROM Pedido e")
    String query) {
        return new PedidoesConverter(getEntities(start, max, query), uriInfo.getAbsolutePath(), expandLevel);
    }

    /**
     * Post method for creating an instance of Pedido using XML as the input format.
     *
     * @param data an PedidoConverter entity that is deserialized from an XML stream
     * @return an instance of PedidoConverter
     */
    @POST
    @Consumes({"application/xml", "application/json"})
    public Response post(PedidoConverter data) {
        Pedido entity = data.resolveEntity(em);
        createEntity(data.resolveEntity(em));
        return Response.created(uriInfo.getAbsolutePath().resolve(entity.getIdPedido() + "/")).build();
    }

    /**
     * Returns a dynamic instance of PedidoResource used for entity navigation.
     *
     * @return an instance of PedidoResource
     */
    @Path("{idPedido}/")
    public PedidoResource getPedidoResource(@PathParam("idPedido")
    Integer id) {
        pedidoResource.setId(id);
        pedidoResource.setEm(em);
        return pedidoResource;
    }

    /**
     * Returns all the entities associated with this resource.
     *
     * @return a collection of Pedido instances
     */
    protected Collection<Pedido> getEntities(int start, int max, String query) {
        return em.createQuery(query).setFirstResult(start).setMaxResults(max).getResultList();
    }

    /**
     * Persist the given entity.
     *
     * @param entity the entity to persist
     */
    protected void createEntity(Pedido entity) {
        entity.setIdPedido(null);
        em.persist(entity);
        Usuario usuario = entity.getUsuario();
        if (usuario != null) {
            usuario.getPedidoCollection().add(entity);
        }
        for (ItemPedido value : entity.getItemPedidoCollection()) {
            Pedido oldEntity = value.getPedido();
            value.setPedido(entity);
            if (oldEntity != null) {
                oldEntity.getItemPedidoCollection().remove(value);
            }
        }
    }
}
